//添加分类
$('#addCategory').on('submit',function(){
    var formData = $(this).serialize();
    $.ajax({
        type:'post',
        url:'/categories',
        data:formData,
        success:function(){
            location.reload();
        }
    });
    return false;
});

//展示添加的分类列表
$.ajax({
    type:'get',
    url:'/categories',
    success:function(response){
        var html = template('categoryTpl',{data:response})
        $('#categoryList').html(html);
    }
});

//为编辑按钮添加点击事件
$('#categoryList').on('click','.edit',function(){
    var id = $(this).attr('data-id');
    $.ajax({
        type:'get',
        url:'/categories/'+id,
        success:function(response){
            console.log(response);
            var html = template('modifyCategoryTpl',response);
            $('#formBox').html(html);
        }
    });
    //当修改分类数据表单发生提交行为时
    $('#formBox').on('submit','#modifyCategory',function(){
        var formData = $(this).serialize();
        var id = $(this).attr('data-id');
        $.ajax({
            type:'put',
            url:'/categories/'+id,
            data:formData,
            success:function(response){
                location.reload();
            }
        });
    });
})

//使用委托事件添加删除的点击事件

$('#categoryList').on('click','.delete',function(){
    var id = $(this).attr('data-id');
    if(confirm('您确定要删除吗？')){
        $.ajax({
            type:'delete',
            url:'/categories/'+id,
            success:function(){
                location.reload();
            }
        });
    }
})

